/*
 * 文件名： YuGaoActivity.java
 * 描    述：预告
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.miguo.activity.base.BaseListActivity;
import com.miguo.activity.callback.IGoods;
import com.miguo.activity.callback.IGoodsScroll;
import com.miguo.activity.callback.listener.GoodsScrollListener;
import com.miguo.adapter.YuGaoAdapter;
import com.miguo.app.AppHelper;
import com.miguo.bean.CommentBean;
import com.miguo.bean.GoodsBean;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.component.BaseStatusView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.ui.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YuGaoActivity extends BaseListActivity<GoodsBean, YuGaoAdapter> implements IGoodsScroll, IGoods<GoodsBean, YuGaoAdapter>, OnClickListener {

    private BaseHeadView mBaseHead;
    private RelativeLayout mContent;
    private BaseStatusView BaseStatus;
    private TextView mNolistText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yugao);
        setGoodsCallBack(this);
        findViewById();
        initView();
    }

    @Override
    protected void findViewById() {
        super.findViewById();

        mContent = (RelativeLayout) findViewById(R.id.content_view);
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setTitle("明日预告");
        // 提醒
        mBaseHead.getRightView().setVisibility(View.VISIBLE);
        mBaseHead.getRightView().setImageResource(R.drawable.top_clock_icon);
        mBaseHead.getRightView().setOnClickListener(this);

        BaseStatus = (BaseStatusView) findViewById(R.id.base_status_view);
        BaseStatus.setContentView(mContent);
        BaseStatus.getRefreshView().setOnClickListener(this);
        BaseStatus.getNetworkView().setOnClickListener(this);

        mNolistText = (TextView) findViewById(R.id.item_nolist_text);
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            // 右边点击
            case R.id.right_btn:
                // 我的收藏
                AppHelper.StartFavor(mActivity);
                break;
            case R.id.refresh_rl:
                this.onGoodsLoadData();
            case R.id.network_error_ll:
                this.onGoodsLoadData();
                break;
        }
        super.onClick(view);
    }

    @Override
    public YuGaoAdapter onGoodsLoadAdapter() {
        return new YuGaoAdapter(mActivity);
    }

    @Override
    public List<GoodsBean> onGoodsDealList(JsonData data) {
        return CommentBean.GoodsData(data.optJson("item"));
    }

    @Override
    public void onGoodsLoadData() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("page", String.valueOf(page));
        HttpClient.post(Url.toRoute(Url.Item.Yugao, 1), param,
                AjaxCallBack);
    }

    @Override
    public GoodsScrollListener onGoodsScroll() {
        return new GoodsScrollListener(this);
    }

    @Override
    public void onGoodsRefresh() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onGoodsFinish() {
        BaseStatus.setRefreshVisibility(View.GONE);
    }

    @Override
    public void onGoodsError() {
        pullToRefresh.setVisibility(View.GONE);
        mNolistText.setVisibility(View.VISIBLE);
        mNolistText.setText(Html.fromHtml("<div style=\"margin:0 auto;text-align:center;\" ><font color=\"#999999\" style=\"margin-left:-15px;\" size=\"2\">明日预告</font><br><b><font color=\"#ff3366\" size=\"2\">每天16:00准时更新！</font></b><br/><font size=\"2\" color=\"#999999\">我在这里等你哦~</font></b></div>"));
    }

    @Override
    public void onGoodsFail() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onScrollIdle() {
        imageLoader.resume();
    }

    @Override
    public void onScrollFling() {
        if (pauseOnFling) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTouchScroll() {
        if (pauseOnScroll) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTopPositiont() {
        // 判断滚动到顶部
        if (mToTopBtn.getVisibility() == View.VISIBLE) {
            mToTopBtn.setVisibility(View.GONE);
            mToTopBtn.startAnimation(mTopDownAnima);
        }
    }

    @Override
    public void onScrollFirstLastPosition() {
        // 第一次滚动至底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
    }

    @Override
    public void onScrollLastPosition() {
        // 判断滚动到底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
        if (pageSize >= page + 1) {
            page++;
            isRefresh = false;
            this.onGoodsLoadData();
        } else {
            Toast.show(mContext, "已经到底了!");
            pullToRefresh.onRefreshComplete();
        }
    }

}
